package com.example.demo.angio.medicine.impl;

import com.example.demo.angio.common.Utils;
import com.example.demo.angio.medicine.MedDto;
import com.example.demo.angio.medicine.MedService;
import com.example.demo.angio.medicine.Medicine;
import com.example.demo.angio.medicine.dao.MedDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional

public class MedServiceImpl implements MedService {
    public List<Medicine> getMedList()
    {

        SqlSession sess= Utils.openSession();

        try
        {
            MedDao dao=sess.getMapper(MedDao.class);
            List<Medicine> medList=dao.findMedList();

            sess.commit();

            return medList;
        }catch(Exception e)
        {
            sess.rollback();
            throw new RuntimeException("查询药物信息失败。",e);
        }
        finally
        {
            sess.close();
        }
    }

    //验证查询是否成功的方法
    public boolean checkMedicine(MedDto dto) {
        SqlSession sqlSession= Utils.openSession();

        try
        {
            MedDao dao= sqlSession.getMapper(MedDao.class);

            int count=dao.findMedCountByNameAndFactory(dto);

            sqlSession.commit();

            return count>0;
        }catch(Exception e)
        {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally
        {
            sqlSession.close();
        }
    }
}
